package com.qf.controller.dangan_controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.dangan.Person;
import com.qf.bean.dangan.ResultVO;
import com.qf.bean.dangan.Users;
import com.qf.bean.dangan.dept;
import com.qf.service.dangan_service.PersonService;
import com.qf.service.dangan_service.UsersService;
import com.qf.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
            敲得越多，钱越多！！！
            敲得越多，钱越多！！！
            敲得越多，钱越多！！！
*/
@Controller
public class PersonController {

    @Autowired
    private PersonService personService;
    @Autowired
    private UsersService usersService;

    @RequestMapping("/getAllPerson")
    public String findAll(@RequestParam(defaultValue = "1")int index, @RequestParam(defaultValue = "2") int pageSize, String username, String deptname, @DateTimeFormat(pattern = "yyyy-MM-dd") Date timename, ModelMap map){
        PageInfo<Person> users1 = personService.findUsers(index, pageSize,username,deptname,timename);
//        在这里可以知道stus为ftl页面的数据源
        List<Person> list = users1.getList();
        map.addAttribute("pageinfo",users1);
        map.addAttribute("pageSize",pageSize);
        map.addAttribute("index1",index);
        map.addAttribute("username",username);
        map.addAttribute("deptname",deptname);
        map.addAttribute("timename",timename);
        //此处因为配置了FreeMarker，所以自动为show补全的为/
        return "show2";
    }


    @RequestMapping("/addPerson")
    public String add(Person person){
        personService.addpost(person);
        return "getAllPerson";
    }

    //批量删除和导出
    @RequestMapping("/deletebyids")
    public void deletes(int[] empid, HttpServletResponse response, String method){

        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            if(method.equals("delete")){
                //此处的empid的数组为form表单提交后得到的多个name为empid的参数
                //删除被选中的id信息!!!
                //此句不能放在try的外面。因为这样不管走那个if语句，都会走这一句，这一句回把选中项的state属性置为0！！！
                int deletes = personService.deletes(empid);
                if(deletes>0){
                    out.print("<script>alert('删除成功');location.href='/getAllPerson'</script>");
                }else {
                    out.print("<script>alert('删除失败');location.href='/getAllPerson'</script>");
                }
            }else if(method.equals("daochu")) {
                //导出数据!!!
                //1、查询被选中的数据信息!!!
                List<Person> findbyids = personService.findbyids(empid);
                //2、导出数据
                //2.1   为表起名字
                ExcelUtils.createFirst("用户信息");

//                把表的数据放进去
                ExcelUtils.createOthers(findbyids);
                //得到导出时候的时间,为下面的文件名做准备
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
                //设置流的导出地并且为.xls文件命名
                ExcelUtils.export(new FileOutputStream("f://"+(simpleDateFormat.format(new Date()))+".xls"));
                out.print("<script>alert('导出成功');location.href='/getAllPerson'</script>");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改密码跳转处
    @RequestMapping("/updatepassword1")
    public String updatepassword1(Integer pid,ModelMap map){
        map.addAttribute("pid",pid);
        return "password2";
    }

    //修改密码
    @RequestMapping("/updatepassword")
    public void updatepassword(String ppassword,Integer pid,HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        try {
            System.out.println(ppassword+pid);
            PrintWriter out = response.getWriter();
            int updatepassword = personService.updatepassword(ppassword, pid);
            if(updatepassword>0){
                out.print("<script>alert('重置成功');location.href='/getAllPerson'</script>");
            }else {
                out.print("<script>alert('重置失败');location.href='/getAllPerson'</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @RequestMapping("/beforeupdateperson")
    public String beforeupdate(int pid, ModelMap map){
        List<dept> dept = usersService.findDept();
        Person updatepost = personService.updateperson(pid);
        Users updatepost1 = usersService.updatepost(updatepost.getUid());
        map.addAttribute("depts",dept);
        map.addAttribute("users",updatepost);
        map.addAttribute("user",updatepost1);
        return "updateperson2";
    }


    @RequestMapping("/updateperson")
    public String update(Person person){
        int i = personService.updateperson1(person);
        return "getAllPerson";
    }

    //删除职位
    //此处为设以json来返回数据,
    @ResponseBody
    @RequestMapping("/deletepenson")
    public ResultVO delete(int pid, HttpServletResponse response){
        int delete = personService.deletepenson(pid);
        //返回
        if (delete==1){
            return new ResultVO(0, "删除成功！", null);
        }
        return new ResultVO(1, "删除失败！", null);
    }
}
